import http from "../../../utils/http"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    columns: ['vue', 'React', 'uniapp', 'Js', 'CSS'],
    selectTypeVisible : false,
    id:"",
    type: "",
    title:"",
    content:"",
  },
  onLoad(){
    const userInfo = wx.getStorageSync("userInfo");
    if(!userInfo){
      wx.redirectTo({
        url: '/pages/login/login',
      })
    }
  },
  onClickLeft(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  onSelectType(){
   this.setData({
    selectTypeVisible:!this.data.selectTypeVisible
   })
  },
  onConfirm(event){
    this.setData({
      type:event.detail.value,
      selectTypeVisible:!this.data.selectTypeVisible
     });
  },
  onCancel(){
    this.setData({
      selectTypeVisible:!this.data.selectTypeVisible
     })
  },
  // 提交
 async handleSubmit(){
  try {
    // 发送请求
    const { title, type,content } = this.data;
    const userInfo = wx.getStorageSync("userInfo");
    const userId = userInfo.id;
    const author = userInfo.name;
    const res = await http.post('/blog/save', {
      userId,
      title,
      type,
      author,
      content    
    });
    if (res.code == 200) {
      wx.reLaunch({
        url: '/pages/index/index'
      });
    }
  } catch (error) {
    // 失败
    wx.showToast({
      title:'保存失败',
      icon: 'none'
    });
  } 
  }
})